import os
import shutil

class Terminal:
    def __init__(self) -> None:
        self.choose = 0
        while True:
            print("------------------------------")
            print("1、物流App发版")
            print("2、生产终端发版")
            print("3、退出")
            self.choose = int(input("请选择: "))
            if self.choose < 1 or self.choose > 3:
                continue
            else:
                if self.choose == 1:
                    self.wuliu()
                elif self.choose == 2:
                    self.terminal()
                elif self.choose == 3:
                    pass                        
            
                break    
        

        pass
    
    def wuliu(self):
        self.apkPath = "D:/Porray/HolosCloud/Porray.HolosCloud.Laundry/Porray.HolosCloud.Laundry.Android/puruiapp/app/build/outputs/apk/xss/release/"
        self.apkTargetPath = "D:/Porray/HolosCloud/Porray.HolosCloud.Laundry/Porray.HolosCloud.Laundry.Android/Release/洗唰唰/com.Porray.Raymobile.mobile/"
        self.compilePath = "D:/Porray/HolosCloud/Porray.HolosCloud.Laundry/Porray.HolosCloud.Laundry.Android/puruiapp"        
        self.configPath = "D:/Porray/HolosCloud/Porray.HolosCloud.Laundry/Porray.HolosCloud.Laundry.Android/puruiapp/config.gradle"

        self.doAction()
        pass

    def terminal(self):
        self.apkPath = "D:/Porray/HolosCloud/Porray.HolosCloud.Laundry/Porray.HolosCloud.Laundry.Android/puruiapp-terminal/app/build/outputs/apk/xss/release/"
        self.apkTargetPath = "D:/Porray/HolosCloud/Porray.HolosCloud.Laundry/Porray.HolosCloud.Laundry.Android/Release/洗唰唰/com.Porray.Raymobile.terminal/"
        self.compilePath = "D:/Porray/HolosCloud/Porray.HolosCloud.Laundry/Porray.HolosCloud.Laundry.Android/puruiapp-terminal"        
        self.configPath = "D:/Porray/HolosCloud/Porray.HolosCloud.Laundry/Porray.HolosCloud.Laundry.Android/puruiapp-terminal/config.gradle"

        self.doAction()
        pass

    def doAction(self):
        # 更新版本
        print("正在更新版本...")
        self.updateVersion(self.configPath)

        # 编译 apk
        print("正在编译...")
        os.chdir(self.compilePath)
        os.system("gradlew.bat :app:assembleXssRelease")

        # 获取新 apk 路径
        self.apkName = ""
        for fileName in os.listdir(self.apkPath):
            if fileName.endswith(".apk"):
                self.apkName = fileName
        

        # 复制 apk 到 Release
        print("正在复制 apk 到发版目录...")
        shutil.copyfile(self.apkPath + self.apkName, self.apkTargetPath + self.apkName)
        print("{}->{}".format(self.apkPath + self.apkName, self.apkTargetPath + self.apkName))
        os.startfile(self.apkTargetPath)

    def updateVersion(self, configPath):
        versionCode = 0
        versionName = 0.0        
        lines = []

        # 读配置
        with open(configPath, 'r', encoding='UTF-8') as f:
            lines = f.readlines()

        lineIdx = 0

        # 修改配置
        for line in lines:
            try:
                idx = line.index("versionCode")
                if idx != -1:
                    idxEnd = line.index(",", idx)
                    versionCodeFull = line[0:idxEnd]
                    prefix = versionCodeFull.split(":")[0]
                    versionCode = (int)(versionCodeFull.split(":")[1])
                    print("{}:{}->{}".format(prefix,versionCode, versionCode + 1))
                    versionCodeFull = "{}:{},\n".format(prefix,versionCode + 1)
                    lines[lineIdx] = versionCodeFull                
            except:
                pass
            
            try:
                idx = line.index("versionName")
                if idx != -1:
                    idxEnd = line.index(",", idx)
                    versionNameFull = line[0:idxEnd]
                    prefix = versionNameFull.split(":")[0]
                    versionName = (versionNameFull.split(":")[1])
                    versionName = (versionName.replace("\"",""))
                    versionNameNew = self.versionAdd(versionName)
                    print("{}:{}->{}".format(prefix,versionName,versionNameNew))

                    versionNameFull = "{}:\"{}\",\n".format(prefix,versionNameNew)
                    lines[lineIdx] = versionNameFull
            except:
                pass
            lineIdx = lineIdx + 1

        # 写配置
        with open(configPath, 'w', encoding='UTF-8') as f:
            f.writelines(lines)
    
    def versionAdd(self,oldVersion):
        arr = oldVersion.split(".")
        arr[1] = f"{int(arr[1]) + 1}"
        if int(arr[1]) > 9: # 进位
            arr[0] = f"{int(arr[0]) + 1}"
            arr[1] = "0"

        oldVersion = ".".join(arr)
        return oldVersion

Terminal()

